package com.chpro.common;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 * Bean转Map or Map转Bean
 * @author chpro
 *
 */
public class BeanUtil {
    private static final Log logger = LogFactory.getLog(BeanUtil.class);

    /**
     * Map转JAVA类
     * @param type
     * @param map
     * @return
     */
    public static <T> T convertMap(Class<T> type, Map map){
        T obj = null;
        try{
            BeanInfo beanInfo = Introspector.getBeanInfo(type);
            obj = type.newInstance();
            PropertyDescriptor[] propertyDescriptors =  beanInfo.getPropertyDescriptors();
            for (int i = 0; i< propertyDescriptors.length; i++) {
                PropertyDescriptor descriptor = propertyDescriptors[i];
                String propertyName = descriptor.getName();

                if (map.containsKey(propertyName)) {
                    Object value = map.get(propertyName);

                    Object[] args = new Object[1];
                    args[0] = value;

                    descriptor.getWriteMethod().invoke(obj, args);
                }
            }
        }catch(Exception e){
            logger.error("Map转java异常,map:"+map+",javaclass"+type);
        }
        return obj;
    }

    /**
     * JAVA类转Map
     * @param bean
     * @return
     */
    public static Map<String,String> convertBean(Object bean){
        Map<String,String> returnMap = new HashMap<String,String>();
        try{
            Class type = bean.getClass();
            BeanInfo beanInfo = Introspector.getBeanInfo(type);

            PropertyDescriptor[] propertyDescriptors =  beanInfo.getPropertyDescriptors();
            for (int i = 0; i< propertyDescriptors.length; i++) {
                PropertyDescriptor descriptor = propertyDescriptors[i];
                String propertyName = descriptor.getName();
                if (!propertyName.equals("class")) {
                    Method readMethod = descriptor.getReadMethod();
                    Object result = readMethod.invoke(bean, new Object[0]);
                    if (result != null) {
                        returnMap.put(propertyName, String.valueOf(result));
                    } else {
                        returnMap.put(propertyName, "");
                    }
                }
            }
        }catch(Exception e){
            logger.error("java转Map异常,javaclass:"+bean);
        }
        return returnMap;
    }

    /**
     * JAVA类转Map排除某些属性
     * @param bean
     * @param params
     * @return
     */
    public static Map<String,String> convertBean(Object bean,String... params){
        Map<String,String> returnMap = new HashMap<String,String>();
        try{
            Class type = bean.getClass();
            BeanInfo beanInfo = Introspector.getBeanInfo(type);

            PropertyDescriptor[] propertyDescriptors =  beanInfo.getPropertyDescriptors();
            for (int i = 0; i< propertyDescriptors.length; i++) {
                PropertyDescriptor descriptor = propertyDescriptors[i];
                String propertyName = descriptor.getName();

                if(Arrays.asList(params).contains(propertyName)){
                    continue;
                }

                if (!propertyName.equals("class")) {
                    Method readMethod = descriptor.getReadMethod();
                    Object result = readMethod.invoke(bean, new Object[0]);
                    if (result != null) {
                        returnMap.put(propertyName, String.valueOf(result));
                    } else {
                        returnMap.put(propertyName, "");
                    }
                }
            }
        }catch(Exception e){
            logger.error("java转Map异常,javaclass:"+bean);
        }
        return returnMap;
    }
}
